package com.app.couponsharelife.ui.main.shop

import android.content.Context
import android.content.Intent
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentPagerAdapter
import com.app.couponsharelife.R
import com.app.couponsharelife.base.BaseActivity
import com.app.couponsharelife.data.bean.HomeClassBean
import com.app.couponsharelife.data.bean.RemoteResultBean
import com.app.couponsharelife.data.remote.retrofit.RetrofitWorker
import com.app.couponsharelife.data.remote.retrofit.exception.OtherException
import com.app.couponsharelife.ui.main.home.search.SearchSelecteActivity
import com.app.couponsharelife.ui.view.ScaleTransitionPagerTitleView
import com.app.couponsharelife.ui.viewfactory.JDFragmentFactory
import com.app.couponsharelife.utils.AppUtils
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.observers.DisposableObserver
import io.reactivex.schedulers.Schedulers
import kotlinx.android.synthetic.main.activity_jdshop.*
import kotlinx.android.synthetic.main.base_title_white.*
import net.lucode.hackware.magicindicator.ViewPagerHelper
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator

/**
 *Created by xct on 2019/1/16
 *Description:京东商品
 */
class JDShopActivity:BaseActivity() {
    private var commonNavigator: CommonNavigator?=null
    private var classList= arrayListOf<HomeClassBean>()
    private var jdFragmentFactory:JDFragmentFactory?=null
    override val viewLayoutId: Int
        get() = R.layout.activity_jdshop

    override fun initViews() {
        AppUtils.initImmersionBar(this,true)
        basetitle_title.text="京东"
        getData()
        basetitle_llclose.setOnClickListener { finish() }
        jdshop_llsearch.setOnClickListener {
            val intent=Intent(this, SearchSelecteActivity::class.java)
            intent.putExtra("Type",0)
            intent.putExtra("GoodsType","JD")
            startActivity(intent)
        }
    }
    private fun setViewPager(){
        jdFragmentFactory=JDFragmentFactory()
        jdshop_viewpager.offscreenPageLimit=6
        jdshop_viewpager.adapter=object : FragmentPagerAdapter(supportFragmentManager){
            override fun getItem(position: Int): Fragment {
                return jdFragmentFactory!!.createFragment(position,classList[position])
            }

            override fun getCount(): Int {
                return classList.size
            }

            override fun getPageTitle(position: Int): CharSequence? {
                return classList[position].name
            }
        }
        commonNavigator= CommonNavigator(this)
        commonNavigator!!.adapter=object : CommonNavigatorAdapter(){
            override fun getTitleView(p0: Context, p1: Int): IPagerTitleView {
                val colorTransitionPagerTitleView= ScaleTransitionPagerTitleView(p0,1f)
                colorTransitionPagerTitleView.normalColor = resources.getColor(R.color.gray99)
                colorTransitionPagerTitleView.textSize=18f
                colorTransitionPagerTitleView.paint.isFakeBoldText=true
                colorTransitionPagerTitleView.selectedColor = resources.getColor(R.color.act_main_bot_nav_select_text_color)
                colorTransitionPagerTitleView.text = classList[p1].name
                colorTransitionPagerTitleView.setOnClickListener { jdshop_viewpager.currentItem = p1 }
                return colorTransitionPagerTitleView
            }

            override fun getCount(): Int {
                return classList.size
            }

            override fun getIndicator(p0: Context?): IPagerIndicator? {
                val indicator= LinePagerIndicator(p0)
                indicator.setColors(resources.getColor(R.color.act_main_bot_nav_select_text_color))
                indicator.mode=LinePagerIndicator.MODE_WRAP_CONTENT
                return indicator
            }
        }
        jdshop_tablayout.navigator=commonNavigator
        ViewPagerHelper.bind(jdshop_tablayout, jdshop_viewpager)
    }
    private fun getData(){
        showProgressDialog("加载中...")
        val map= HashMap<String,String>()
        RetrofitWorker.retrofitWorker.getJDClass(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(object : DisposableObserver<RemoteResultBean<List<HomeClassBean>>>(){
                    override fun onComplete() {

                    }

                    override fun onNext(it: RemoteResultBean<List<HomeClassBean>>) {
                        dismissProgressDialog()
                        if (it.code==200){
                            if (it.data!!.isNotEmpty()){
                                classList.clear()
                                val myHomeClassBean=HomeClassBean()
                                myHomeClassBean.name="精选"
                                classList.add(myHomeClassBean)
                                classList.addAll(it.data!!)
                                setViewPager()
                            }
                        }
                    }

                    override fun onError(e: Throwable) {
                        dismissProgressDialog()
                        if (e is OtherException){
                            showToastMsg(e.msg)
                        }else{
                            showToastMsg("网络加载出错了！")
                        }
                    }

                })
    }

    override fun onDestroy() {
        super.onDestroy()
        AppUtils.destroyImmersionBar(this)
    }
}